const db = wx.cloud.database({
  env: "treeworld-2g5yvz9s899bf4a6"
})
const app = getApp()
var startY = 0,
  moveY = 0,
  pageAtTop = true;
Component({


  /**
   * 组件的初始数据
   */
  data: {
    show3: true,
    countDownNum: '1',
    avatarUrl: "https://s1.ax1x.com/2020/07/28/aAdel6.jpg",
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    nickName: "游客",
    show: false,
    coverTransform: "translateY(0px)",
    coverTransition: "0s",
    moving: !1
  },
  pageLifetimes: {
    show: function () {
      this.sq()
    },

  },
  created(){
    this.sq()
   console.log( wx.getStorageSync( 'userid' )) 
  },


  methods: {
    sz() {
      wx.openSetting({
        success: function (res) {}
      })

    },
    ql() {
      wx.showToast({
        title: '清理缓存成功',
        icon: 'none',
      })
      // this.logout()
      wx.clearStorage()
    },

    sq2() {
      var user = app.globalData.user
      this.data.setData({
        list: user
      })
    },
    countDown() {
      let that = this;
      var user = app.globalData.user
      let countDownNum = that.data.countDownNum; //获取倒计时初始值
      //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
      that.setData({
        timer: setInterval(function () { //这里把setInterval赋值给变量名为timer的变量
          //每隔一秒countDownNum就减一，实现同步
          countDownNum--;
          //然后把countDownNum存进data，好让用户知道时间在倒计着
          that.setData({
            countDownNum: countDownNum
          })
          console.log(countDownNum)
          //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
          if (countDownNum == 0) {
            that.setData({
              list: user
            })
            clearInterval(that.data.timer);
          }
          app.globalData.sydh = 2
        }, 1000)
      })
    },
    // bindGetUserInfo: function (e) {
    //   console.log(e.detail.userInfo)
    //   db.collection('user').add({
    //     data: {
    //       avatarUrl: e.detail.userInfo.avatarUrl,
    //       nickName: e.detail.userInfo.nickName
    //     }
    //   }).then(res => {})
    //   if (e.detail.userInfo) {
    //     this.data.sq()
    //   }
    // },
    hideModal(){
      this.setData({
        show:false
      })
    },
    getUserProfile() {
      var that = this
      let userid = wx.getStorageSync( 'userid' )
  
      app.userInfo()
      wx.getUserProfile({
        desc: '展示用户信息',
        success: (res) => {
          db.collection('user').doc(userid).update({
            data: {
              avatarUrl: res.userInfo.avatarUrl,
              nickName: res.userInfo.nickName,
            }
          }).then(res => {
            this.setData({
              show: false
            })
            this.sq()
          })
        }
      })
    },
    navTo: function navTo(url) {
      if (!this.data.hasLogin) {
        url = '/pages/public/login';
      }
      uni.navigateTo({
        url: url
      });

    },


    coverTouchstart(e) {
      if (this.data.pageAtTop === false) {
        return;
      }
      this.setData({
        coverTransition: 'transform .1s linear',
        startY:e.touches[0].clientY
      })


    },
    coverTouchmove(e) {
      moveY = e.touches[0].clientY;
      var moveDistance = moveY -this.data. startY;
      if (moveDistance < 0) {
        // this.data.moving = false;
        this.data.moving = true;
        if(375+moveDistance>this.data. CustomBar+this.data.StatusBar+140){
          this.setData({
            heights:moveDistance
          })
        }
      }else{
        this.data.moving = true;
        if (moveDistance >= 80 && moveDistance < 100) {
          moveDistance = 80;
          this.setData({
            coverTransform: "translateY(".concat(moveDistance, "px)")
          })
        }
        if (moveDistance > 0 && moveDistance <= 80) {
          this.setData({
            coverTransform: "translateY(".concat(moveDistance, "px)")
          })
        }
      }
 
    },
    coverTouchend() {
      if (this.data.moving === false) {
        return;
      }
      console.log(this.data.heights)
      if(this.data.heights<-140){
        this.setData({ heights:0})
      }
      this.setData({
        moving: false,
        coverTransition: 'transform 0.3s cubic-bezier(.21,1.93,.53,.64)',
        coverTransform: 'translateY(0px)',
      
      })

    },
    logout() {
      var that = this;
      wx.showModal({
        title: '退出提醒',
        content: '是否退出当前登录的码云帐号？',
        showCancel: true,
        confirmText: "退出",
        confirmColor: "#ff4500",
        cancelText: "返回",
        success(res) {
          if (res.confirm) {
            app.logout();
            wx.navigateTo({
              url: "../../pages/user/gitee_login/index"
            })
          }
        }
      });
    },
    async user_list() {
      var sc
      var dz
      var nt
      var tk
      await db.collection('collect').count().then(res => {
        sc = res.total
      })
      await db.collection('statr').count().then(res => {
        dz = res.total
      })
      await db.collection('note').count().then(res => {
        nt = res.total
      })
      await db.collection('interview_collect').count().then(res => {
        tk = res.total
      })
      await this.setData({
        dz: dz,
        sc: sc,
        nt: nt,
        tk:tk
      })
    },
    sq() {
      let that = this
      db.collection('user').get().then(res => {
        console.log(res)
        if (res.data[0].avatarUrl ) {
          this.user_list()
          that.setData({
            avatarUrl: res.data[0].avatarUrl,
            nickName: res.data[0].nickName,
            userin: res.data[0],
          })
        } else{
          this.setData({show:true})
        }
      })
    },
    tz(e) {
      console.log(e.currentTarget.dataset.id)
      wx.navigateTo({
        url: "../../pages/" + e.currentTarget.dataset.a + "?id=" + e.currentTarget.dataset.id
      })
    },

  }
})